package com.oig.sys.workflow.controller;


import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import com.oig.sys.workflow.dto.DataModuleDto;
import com.oig.sys.workflow.service.ITbDataModuleService;
import com.oig.sys.workflow.vo.DataModuleVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Tag(name = "实体对象管理")
@RestController
@RequestMapping("/dataModule")
@Slf4j
@RequiredArgsConstructor
public class TbDataModuleController {

    private final ITbDataModuleService dataModuleService;


    @PreAuthorize("@pms.hasPermission('/dataModule/add')")
    @SysLog(value="添加实体对象",apiUrl="/dataModule/add")
    @Operation(summary = "添加实体对象")
    @PostMapping(value = "/add")
    public DataModuleVo addDataModule(@RequestBody @Validated(GroupAdd.class) DataModuleDto dataModuleDto){
        log.info("add data module, {}", dataModuleDto);
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        dataModuleDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        dataModuleDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        dataModuleDto.setLastUpdateTime(new Date());
        return this.dataModuleService.addDataModule(dataModuleDto);
    }


    @PreAuthorize("@pms.hasPermission('/dataModule/update')")
    @SysLog(value="修改实体对象",apiUrl="/dataModule/update")
    @Operation(summary = "修改实体对象")
    @PostMapping(value = "/update")
    public DataModuleVo updateDataModule(@RequestBody @Validated(GroupEdit.class) DataModuleDto dataModuleDto){
        log.info("update data module, {}", dataModuleDto);
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        dataModuleDto.setLastUpdateId(SecurityUtil.getDefineUserMain().getId());
        dataModuleDto.setLastUpdateName(SecurityUtil.getDefineUserMain().getRealName());
        dataModuleDto.setLastUpdateTime(new Date());
        return this.dataModuleService.updateDataModule(dataModuleDto);
    }

    @PreAuthorize("@pms.hasPermission('/dataModule/cancel')")
    @SysLog(value="注销实体对象",apiUrl="/dataModule/cancel")
    @Operation(summary = "注销实体对象")
    @PostMapping(value = "/cancel/{moduleId}")
    public SysResponse<Integer> cancelDataModule(@PathVariable("moduleId") Integer moduleId){
        log.info("cancel data module id={}", moduleId) ;
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        this.dataModuleService.cancelDataModule(moduleId);
        return SysResponse.ok(moduleId,"注销实体对象成功");
    }


    @PreAuthorize("@pms.hasPermission('/dataModule/reuse')")
    @SysLog(value="恢复实体对象",apiUrl="/dataModule/reuse")
    @Operation(summary = "恢复实体对象")
    @PostMapping(value = "/reuse/{moduleId}")
    public SysResponse<Integer> reuseDataModule(@PathVariable("moduleId") Integer moduleId){
        log.info("reuse data module id={}", moduleId) ;
        if (SecurityUtil.getDefineUserMain().getTenantId()!=0){
            throw new BadRequestException("租户无权操作");
        }
        this.dataModuleService.reuseDataModule(moduleId);
        return SysResponse.ok(moduleId,"恢复实体对象成功");
    }


    @Operation(summary = "查看实体对像详细信息")
    @PostMapping(value = "/info/{moduleId}")
    public DataModuleVo getDataModule(@PathVariable("moduleId") Integer moduleId){
        log.info(" get data module id=:{}", moduleId);
        return this.dataModuleService.getDataModule(moduleId);
    }

    @Operation(summary = "根据表名查询实体对像详细信息")
    @PostMapping(value = "/getByTableName")
    public DataModuleVo getByTableName(@RequestParam("tableName") String tableName){
        log.info(" get data module tableName=:{}", tableName);
        return this.dataModuleService.getByTableName(tableName);
    }


    @Operation(summary = "查看实体对像列表,提供备选")
    @PostMapping("/list")
    public List<DataModuleVo> list(@RequestBody SysRequest<DataModuleDto> dataModuleRequest){
        log.info("list data module :{}",dataModuleRequest);
        return this.dataModuleService.queryList(dataModuleRequest);
    }

}
